//
//  DSZTopicViewController.m
//  7.13苏宁框架
//
//  Created by app01 on 15/7/16.
//  Copyright (c) 2015年 lovena. All rights reserved.
//

#import "DSZTopicViewController.h"
#import "TopicCellModel.h"
#import "TopicCollectionCell.h"
#import "Common.h"
#import "TopicView.h"
#import "TopicModel.h"
#import "TopicTableCell.h"
#import "DSZContentViewController.h"
#import "DSZShowViewController.h"
#import "DSZMainViewController.h"
#import "DSZSearchViewController.h"
#import "MJChiBaoZiHeader.h"
#import "MBProgressHUD+MJ.h"

static const CGFloat MJDuration = 2.0;
static NSString *const ID = @"topic";

@interface DSZTopicViewController ()<ClickImageDelagate>


@property(nonatomic,strong)DSZShowTabbarView *tabbarView;



//加载BarButtonItem
-(void)loadBarButtonItem;

//加载表头视图
-(void)loadHeaderView;

////加载表视图
-(void)loadTableView;


@end

@implementation DSZTopicViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self loadBarButtonItem];
    [self loadHeaderView];
    [self loadTableView];
    
    _tableView.header = [MJChiBaoZiHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
}

#pragma mark 下拉刷新数据
- (void)loadNewData
{
    
    // 2.模拟2秒后刷新表格UI（真实开发中，可以移除这段gcd代码）
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(MJDuration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // 刷新表格
        
        [MBProgressHUD showSuccess:@"刷新成功"];
        // 拿到当前的下拉刷新控件，结束刷新状态
        [self.tableView.header endRefreshing];
    });
}

-(void)viewWillAppear:(BOOL)animated
{
    
    DSZShowViewController *show = (DSZShowViewController*)self.tabBarController;
    [show hiddenShowTabbar:NO];

    
}


- (IBAction)returnMain:(id)sender {
   
    UIStoryboard *story=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
    DSZMainViewController *vc=[story instantiateViewControllerWithIdentifier:@"main"];
    [self presentViewController:vc animated:NO completion:nil];
    
}

-(void)loadBarButtonItem{
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, 0, 25, 25);
    [btn setBackgroundImage:[UIImage imageNamed:@"search"] forState:UIControlStateNormal];
    [btn setBackgroundImage:[UIImage imageNamed:@"search"] forState:UIControlStateHighlighted];
    [btn addTarget:self action:@selector(search) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithCustomView:btn];
    self.navigationItem.rightBarButtonItem = rightItem;
    
}

//加载表头视图
-(void)loadHeaderView{
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 64, self.view.width,380 )];
    [self.view addSubview:view];
    _headerView = view;
    
    [self loadTopicHeader];
    [self loadCollectionVIew];
    
}

-(void)loadTopicHeader{
    
    
    TopicView *topicView = [[TopicView alloc]init];
    topicView.delegate = self;
    topicView.frame = CGRectMake(0, 0, self.view.width, 150);
    _topicArray = [TopicModel modelArrayWithFilename:@"topic.plist"];
    topicView.topicArray = _topicArray;
    [_headerView addSubview:topicView];
    _topicView = topicView;
}

-(void)loadCollectionVIew{
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(70, 100);
    layout.minimumInteritemSpacing = 6;
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, _topicView.bottom, self.view.width, 230) collectionViewLayout:layout];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    collectionView.showsHorizontalScrollIndicator = NO;
    collectionView.showsVerticalScrollIndicator = NO;
    collectionView.scrollEnabled = NO;
    collectionView.backgroundColor = [UIColor whiteColor];
    UINib *nib = [UINib nibWithNibName:@"TopicCollectionCell" bundle:nil];
    [collectionView registerNib:nib forCellWithReuseIdentifier:ID];
    [_headerView addSubview:collectionView];
    _collectionView = collectionView;
    
    //加载数据
    _collectionArray = [NSArray array];
    _collectionArray = [TopicCellModel modelArrayWithFilename:@"topicCollectionCell.plist"];
    
}


////加载表视图
-(void)loadTableView{
    
    UITableView *tableView = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStyleGrouped];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.showsVerticalScrollIndicator = NO;
    tableView.tableHeaderView = _headerView;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:tableView];
    _tableView = tableView;
    
}

-(void)search{
    

    
    UIStoryboard *story=[UIStoryboard storyboardWithName:@"Topic" bundle:nil];
    DSZSearchViewController *serchVC=[story instantiateViewControllerWithIdentifier:@"search"];
    serchVC.array = _collectionArray;
    [self.navigationController pushViewController:serchVC animated:NO];
    DSZShowViewController *show=(DSZShowViewController*)self.tabBarController;
    [show hiddenShowTabbar:YES];
}


#pragma mark 点击图片代理方法
-(void)tapBtn:(UIButton *)btn{
    
    DSZContentViewController *contentVC = [[DSZContentViewController alloc]init];
    //NSLog(@"%i",btn.tag);
    TopicModel *model =_topicArray[btn.tag-10];
    contentVC.headerModel = model;
    contentVC.cotentCollectionArray = model.contents;
    [self.navigationController pushViewController:contentVC animated:YES];
}


#pragma mark collectionView代理方法

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _collectionArray.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    TopicCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    cell.topicModel = _collectionArray[indexPath.item];
    return cell;
}
//选中九宫格事件
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    DSZContentViewController *contentVC = [[DSZContentViewController alloc]init];
    TopicCellModel *cellmodel =_collectionArray[indexPath.item];
    
    contentVC.headerModel = (TopicModel *)cellmodel;
    contentVC.cotentCollectionArray = cellmodel.contents;
    [self.navigationController pushViewController:contentVC animated:YES];
    DSZShowViewController *show = (DSZShowViewController*)self.tabBarController;
    [show hiddenShowTabbar:YES];
}

#pragma mark tableView代理方法

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _topicArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *tableID = @"cell";
    TopicTableCell *cell = [tableView dequeueReusableCellWithIdentifier:tableID];
    if (cell == nil) {
        cell = [[TopicTableCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tableID];
    }
    cell.topicModel = _topicArray[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return  cell;
}
//单元格高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return  170;
}

//段头高
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40;
}
//自定义段头
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[NSBundle mainBundle]loadNibNamed:@"SectionHeader" owner:self options:nil][0];
      return view;
}


//选中表格事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    DSZContentViewController *contentVC = [[DSZContentViewController alloc]init];
    TopicModel *model =_topicArray[indexPath.row];
    contentVC.headerModel = model;
    contentVC.cotentCollectionArray = model.contents;
    [self.navigationController pushViewController:contentVC animated:YES];

}


@end
